package com.aion.jirc.gui.windows;

import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import java.awt.BorderLayout;

/**
 * Created by IntelliJ IDEA.
 * User: Aion
 * Date: 11-02-21
 * Time: 21:41
 */
public abstract class Window extends JInternalFrame {

    private String windowName;
    private boolean isChannel;
    private boolean hasOutputEnabled;

    protected InputArea inputArea;
    protected JTextField outputArea;
    protected JList nickList;

    protected Window(String windowName, boolean hasOutputEnabled,
                     boolean isChannel) {
        super(windowName, true, true, true, true);
        this.windowName = windowName;
        this.hasOutputEnabled = hasOutputEnabled;
        this.isChannel = isChannel;
        init();
    }

    public final String getWindowName() {
        return windowName;
    }

    private void init() {
        setTitle(windowName);

        inputArea = new InputArea();
        JScrollPane scrollPane = new JScrollPane(inputArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        if (isChannel) {
            JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setResizeWeight(1);
            splitPane.add(scrollPane);

            nickList = new JList();
            splitPane.add(new JScrollPane(nickList, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
            add(splitPane, BorderLayout.CENTER);
        } else {
            add(scrollPane);
        }

        if (hasOutputEnabled) {
            outputArea = new JTextField();
            add(outputArea, BorderLayout.PAGE_END);
        }

        setResizable(true);
        setVisible(true);
    }


    private class InputArea extends JList {

        private InputArea() {
            super();
            setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        }

    }

}
